package com.service.Impl;

import java.util.ArrayList;
import java.util.List;

import com.dao.AreaMapper;
import com.entity.Area;
import com.utils.DataTables;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.AreaService;
import com.utils.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class AreaServiceImpl extends BaseService implements AreaService {
    @Autowired
    private AreaMapper areaMapper;

    @Override
    public long getAreaRowCount() {
        return areaMapper.getAreaRowCount();
    }

    /**
     * 查询所有省市信息
     *
     * @return
     */
    @Override
    public List<Area> selectArea() {
        return areaMapper.selectArea();
    }

    //获取所有省市列表
    @Override
    public DataTables getAreas(DataTables dataTables) {
        PageHelper.startPage(dataTables.getStart(), dataTables.getLength()); // 核心分页代码
        PageHelper.orderBy("sort asc");

        if(!StringUtils.isEmpty(dataTables.getColumn())){
            String column = propertyToColumn(AreaMapper.class,
                    dataTables.getColumn());
            if(!StringUtils.isEmpty(column)){
                PageHelper.orderBy(column + " " + dataTables.getOrder());
            }
        }

        PageInfo<Area> pageInfo = new PageInfo<Area>
                (areaMapper.getAreas(dataTables.getSearch(), dataTables.getSubSQL()));
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ?
                pageInfo.getList() : new ArrayList<Area>());

        return dataTables;
    }

    @Override
    public Area selectAreaByObj(Area obj) {
        return areaMapper.selectAreaByObj(obj);
    }

    @Override
    public Area selectAreaById(Integer id) {
        return areaMapper.selectAreaById(id);
    }

    @Override
    public int insertArea(Area value) {
        return areaMapper.insertArea(value);
    }

    @Override
    public int insertNonEmptyArea(Area value) {
        return areaMapper.insertNonEmptyArea(value);
    }

    @Override
    public int insertAreaByBatch(List<Area> value) {
        return areaMapper.insertAreaByBatch(value);
    }

    @Override
    public int deleteAreaById(Integer id) {
        return areaMapper.deleteAreaById(id);
    }

    @Override
    public int updateAreaById(Area enti) {
        return areaMapper.updateAreaById(enti);
    }

    @Override
    public int updateNonEmptyAreaById(Area enti) {
        return areaMapper.updateNonEmptyAreaById(enti);
    }

    public AreaMapper getAreaMapper() {
        return this.areaMapper;
    }

    public void setAreaMapper(AreaMapper areaMapper) {
        this.areaMapper = areaMapper;
    }

}